package model;

import java.sql.SQLException;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

public class KimchiService {
	private KimchiDao kimchiDao;
	public void setKimchiDao(KimchiDao kimchiDao){
		this.kimchiDao=kimchiDao;
	}
	
	/**
	 * 다음 작업지시번호 받아오기
	 */
	public int getWorkorderNo() throws SQLException {
		int workorderNo=kimchiDao.getWorkorderNo();
		return workorderNo;
	}

	
	/**
	 * 재고현황보기
	 */
	public List<InventoryVO> getInventoryList() throws SQLException {
		return kimchiDao.getInventoryList();
	}
	
	/**
	 * 재료 입고
	 */
	public void insertInventory(InventoryVO ivo) throws SQLException{
		kimchiDao.insertInventory(ivo);
	}

	/**
	 * 작업요청현황보기
	 */
	public WorkOrderVO getWorkOrder() throws SQLException {
		int workOrderNo=kimchiDao.getMaxWorkOrderNo();
		return kimchiDao.getWorkOrder(workOrderNo);
	}

	/**
	 * 작업요청
	 */
	public void workOrder(WorkOrderVO wvo) throws SQLException {
		kimchiDao.workOrder(wvo);
	}

	/**
	 * 작업
	 */
	public void play(HashMap map) throws SQLException {
		int lot_no=0;
		map.put("workorder_no", kimchiDao.getMaxWorkOrderNo());
		if((map.get("station_name")).equals("세척")){
			map.put("inventory_no", kimchiDao.getInventoryNo((String) map.get("station_name")));
			kimchiDao.decreaseInventory(Integer.parseInt(map.get("inventory_no")+""));
			map.put("lot_no", kimchiDao.insertProduct(map));
		}else{
			map.put("inventory_no", kimchiDao.getInventoryNo((String) map.get("station_name")));
			kimchiDao.decreaseInventory(Integer.parseInt(map.get("inventory_no")+""));
			map.put("lot_no", kimchiDao.getProductNo((String) map.get("station_name")));//현재공정의 대기중인 물품 가져오기
			kimchiDao.updateProduct(map);			//작업입력
		}
		kimchiDao.writeHistory(map);				// 히스토리 쓰기
		System.out.println(map);
		String nextStep=kimchiDao.getNextStep(map);	// 다음작업 찾기
		System.out.println("다음작업: "+nextStep);
		if(!(nextStep==null || nextStep=="")){ 		// 다음작업이 있으면
			map.put("station_name", nextStep);
			map.put("condition_name", "대기");
			kimchiDao.updateProduct(map); 			// 다음작업으로 보내기
		}
		if(map.get("station_name").equals("포장") && map.get("condition_name").equals("완료")){ // 작업 완료하면
			kimchiDao.completeProduct((int) map.get("workorder_no"));
		}
	}
	
	/**
	 * 공정내 세부현황
	 */
	public List<ProductVO> getStationProductList(String station_name) throws SQLException {
		List<ProductVO> list=kimchiDao.getStationProductList(station_name);
		return list;
	}
	
	/**
	 * 공정 현황
	 */
	public HashMap getStationInfoCount(String station_name) throws SQLException {
		HashMap param=new HashMap();
		param.put("station_name", station_name);
		param.put("workorder_no", kimchiDao.getMaxWorkOrderNo());
		
		int waitCount=kimchiDao.getStationWait(station_name);
		int completeCount=kimchiDao.getStationComplete(param);
		int errorCount=kimchiDao.getStationError(station_name);
		int total=waitCount+completeCount+errorCount;
		HashMap result=new HashMap();
		result.put("waitCount", waitCount);
		result.put("completeCount", completeCount);
		result.put("errorCount", errorCount);
		result.put("totalCount", total);
		return result;
	}
	
	/**
	 * 불량세부현황 리스트
	 */
	public List<ErrorVO> getStationErrorList(String station_name) throws SQLException {
		return kimchiDao.getStationErrorList(station_name);
	}
	/**
	 * 일일 불량 누적 차트 관련 메서드
	 */
	public ArrayList<ChartVO> cumulativeFailureCharts(ChartVO cvo) throws SQLException {
		return kimchiDao.cumulativeFailureCharts(cvo);
	}
	/**
	 * 불량 세부 차트 관련
	 * @throws SQLException 
	 */
	public ArrayList<BadDetailChartVO> badDetailChart() throws SQLException {
		return kimchiDao.badDetailChart();
	}
	/**
	 * 주간 작업량
	 * @throws SQLException 
	 */
	public ArrayList<ChartVO> workWeek(ChartVO cvo) throws SQLException {
		return kimchiDao.workWeek(cvo);
	}

	/**
	 * 가동현황
	 */
	public List<OperationInfoVO> operationInfo() throws SQLException {
		int workorder_no=kimchiDao.getMaxWorkOrderNo();
		return kimchiDao.operationInfo(workorder_no);
	}
	
	/**
	 * 불량품 정보 찾기
	 */
	public List<ErrorInfoVO> getErrorInfoList() throws SQLException{
		List<ErrorInfoVO> param=kimchiDao.getErrorProduct();
		List<ErrorInfoVO> list=new ArrayList<ErrorInfoVO>();
		for(ErrorInfoVO vo: param){
			String material_name=kimchiDao.getErrorMaterial(vo.getCondition_name());
			ErrorInfoVO input=new ErrorInfoVO();
			input.setLot_no(vo.getLot_no());
			input.setMaterial_name(material_name);
			ErrorInfoVO result=kimchiDao.getErrorInfo(input);
			list.add(result);
		}
		return list;
	}
}
